<?php
/**
 * 联盟商家
 */

namespace Admin\Controller;

use Common\Lib\TagLib\get;
use Think\Page;

class SupplierController extends AdminController {

    public function index() {
        $cpage = (int)$_GET['p'] > 0 ? (int)$_GET['p'] : 1;
        $where = [];
        $nickname = I('nickname', '', 'addslashes,trim');
        $mobile = I('mobile', '', 'addslashes,trim');
        $supplier_name = I('supplier_name', '', 'addslashes,trim');

        if (empty($this->users)) {
            $this->getAllUsers();
        }
        if (!empty($nickname)) {
            $user_id = 0;
            foreach ($this->users as $user) {
                if ($user['nickname'] == $nickname) {
                    $user_id = $user['id'];
                    break;
                }
            }
            $where['user_id'] = $user_id;
        }

        if (!empty($mobile)) {
            $where['mobile'] = $mobile;
        }

        if (!empty($supplier_name)) {
            $where['supplier_name'] = $supplier_name;
        }

        $Supplier = M('SupplierInfo');
        $result = $Supplier->where($where)->order('id DESC')->page($cpage, $this->page_size)->select();

        $count = $Supplier->where($where)->count();
        $Page = new \Think\Page($count, $this->page_size);

        $this->assign([
            'result' => $result,
            'page' => $Page->show(),
            'users' => $this->users,
        ]);
        $this->display();
    }

    public function detail() {
        $id = I('id', 0, 'intval');
        if (empty($id)) {
            $this->error('缺少参数');
            return;
        }
        $Supplier = M('SupplierInfo');
        $info = $Supplier->find($id);
        if (empty($info)) {
            $this->error('没有找到该记录');
            return;
        }

        if (empty($this->users)) {
            $this->getAllUsers();
        }

        $this->assign([
            'result' => $info,
            'users' => $this->users,
        ]);

        $this->display();
    }

    public function ok() {
        $id = I('id', 0, 'intval');
        if (empty($id)) {
            $this->error('缺少参数');
            return;
        }

        $status = I('status', 1, 'intval');
        if (!in_array($status, [1, 2])) {
            $this->error('参数错误');
            return;
        }

        if ($status == 1) {
            $msg = I('refuse_msg', '', 'addslashes');
        }

        $info = M('SupplierInfo')->where(['id' => $id])->find();

        if (empty($info)) {
            $this->error('信息不存在');
            return;
        }
        if ($info['status'] != 0) {
            $this->error('该信息已处理');
            return;
        }

        $data = [];
        $data = [
            'id' => $info['id'],
            'status' => $status,
            'deal_at' => time(),
            'checker' => $this->admin_info['username']
        ];

        M()->startTrans();
        try {
            //更新申请记录状态
            M('SupplierInfo')->data($data)->save();
            if ($data['status'] == 2) {
                //审核通过
                M('User')->where(['id' => $info['user_id']])->save([
                    'identity' => 69
                ]);
                //发消息
                M('Message')->add([
                    'user_id' => $info['user_id'],
                    'title' => '联盟商家申请通过审核',
                    'content' => "恭喜{$this->users[$info['user_id']['nickname']]},您的联盟商家申请已通过审核！",
                    'created_at' => time(),
                ]);
            }
        } catch (\Exception $e) {
            M()->rollback();
            $this->error($e->getMessage());
            return;
        }

        M()->commit();
        $this->success('审核成功');
    }

    public function activity() {
        $cpage = (int)$_GET['p'] > 0 ? (int)$_GET['p'] : 1;
        $keyword = I('keyword', '', 'addslashes,trim');
        $where = [];
        if (!empty($keyword)) {
            $where['A.activity_name'] = ['like', "%{$keyword}%"];
        }
        $result = M('SupplierActivity')->alias('A')->field('A.*,B.nickname')
            ->join(C('DB_PREFIX') . 'user as B ON A.user_id = B.id', 'left')
            ->where($where)->order('id DESC')->page($cpage, $this->page_size)->select();
        $count = M('SupplierActivity')->alias('A')->where($where)->count();
        $Page = new Page($count, $this->page_size);
        $this->assign([
            'result' => $result,
            'page' => $Page->show(),
        ]);
        $this->display();
    }

    public function activity_edit() {
        $id = I('id', 0, 'intval');
        if (empty($id)) {
            return $this->error('参数错误');
        }
        $activity = M('SupplierActivity')->where(['id' => $id])->find();
        if (empty($activity)) {
            return $this->error('没有找到该活动');
        }

        if (IS_POST) {
            $data = [];
            $data['begin_time'] = I('begin_time', '', 'trim');
            $data['end_time'] = I('end_time', '', 'trim');
            $data['activity_name'] = I('activity_name', '', 'addslashes,trim');
            $data['activity_summary'] = I('activity_summary', '', 'addslashes,trim');
            $data['activity_content'] = I('activity_content', '', 'addslashes,trim');
            $data['status'] = I('status', 0, 'tinyintval');
            $data['show_index'] = I('show_index', 0, 'tinyintval');
            $data['recommend'] = I('recommend', 0 ,'intval');
            if (!$data['begin_time'] = strtotime($data['begin_time'])) {
                return $this->error('开始时间格式错误');
            }

            if (!$data['end_time'] = strtotime($data['end_time'])) {
                return $this->error('结束时间格式错误');
            }

            if (!empty($_FILES['thumb'])) {
                $data['thumb'] = $this->uploadImage($_FILES['thumb']);
            }
            $result = M('SupplierActivity')->where(['id' => $activity['id']])->save($data);
            if ($result !== false) {
                return $this->success('活动编辑成功');
            }

            return $this->error('活动编辑失败');
        } else {
            $this->assign($activity);
            $this->display();
        }
    }

    public function over_time() {
        $type = I('_type', 0, 'tinyintval');
        $id = I('id', 0, 'intval');
        if (empty($id)) {
            return $this->error('参数错误');
        }
        if (!in_array($type, [0, 1])) {
            $type = 0;
        }

        $result = M('SupplierActivity')->where(['id' => $id])->save([
            'status' => $type,
        ]);

        if ($result !== false) {
            return $this->success('状态更新成功');
        }

        return $this->error('更新失败');
    }

    public function delete() {
        $id = I('id', 0, 'intval');
        if (empty($id)) {
            return $this->error('参数错误');
        }
        $info = M('SupplierInfo')->where(['id' => $id])->find();
        if (empty($info)) {
            return $this->error('没有找到该记录');
        }
        if ($info['status'] == 2) {
            return $this->error('该记录不能删除');
        }
        $pics = [];
        $pics = [
            getcwd() . $info['business_license'],
            getcwd() . $info['organization_code'],
            getcwd() . $info['id_face'],
            getcwd() . $info['id_back']
        ];
        foreach ($pics as $pic) {
            if (file_exists($pic)) {
                unlink($pic);
            }
        }
        M('SupplierInfo')->where(['id' => $id])->delete();
        return $this->success('删除成功');
    }

    public function activity_delete() {
        $id = I('id', 0, 'intval');
        if (empty($id)) {
            return $this->error('参数错误');
        }

        $activity = M('SupplierActivity')->where([
            'id' => $id,
        ])->find();
        if (empty($activity)) {
            return $this->error('活动不存在');
        }
//        if($activity['end_time'] < time()){
//            return $this->error('该活动还没有结束');
//        }
        if (M('SupplierActivity')->delete($activity['id']) !== false) {
            return $this->success('活动删除成功');
        }

        $this->error('删除失败');
    }

}
